﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "ScaleGraphicsNodeStyleCustomizer.h"
#include "IElement.h"
#include "IDocument.h"
#include "IModelView.h"
#include "IGraphicsStyle.h"
#include "IDocumentManager.h"
#include "IUserTransaction.h"
#include "ILinkElementProxy.h"
#include "IGraphicsStyleData.h"
#include "ILinkDocumentElement.h"
#include "IGraphicsStyleManager.h"
#include "GraphicsNodeVisibility.h"
#include "CustomizerGStyleManager.h"
#include "IElementBasicInformation.h"
#include "GcmpBuiltInCategoryUniIdentities.h"
#include "IGraphicsNodeStyleAndMaterialCustomizer.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace Sample;

//自定义样式节点
DBOBJECT_DATA_DEFINE(ScaleGraphicsNodeStyleCustomizer)
{
    m_pOwnerElement = nullptr;
}

ScaleGraphicsNodeStyleCustomizer::ScaleGraphicsNodeStyleCustomizer(IElement* pElement)
    :m_pOwnerElement(pElement)
{

}

IGraphicsNodeStyleAndMaterialCustomizer::CustomizationInfo ScaleGraphicsNodeStyleCustomizer::GetCustomizationInfo(const IModelView* pModelView) const
{
    IGraphicsNodeStyleAndMaterialCustomizer::CustomizationInfo info;
    DBG_WARN_AND_RETURN_UNLESS(pModelView, info, L"pModelView 为空",L"GDMP",L"2024-03-30");
    DBG_WARN_AND_RETURN_UNLESS(m_pOwnerElement, info, L"m_pOwnerElement为空",L"GDMP",L"2024-03-30");
    IDocument* pDocument = m_pOwnerElement->GetDocument();
    DBG_WARN_AND_RETURN_UNLESS(pDocument, info, L"pDocument为空",L"GDMP",L"2024-03-30");

    // 获得两个GDMP内建的类别样式
    IGraphicsStyleManager* pGStyleManager = pDocument->GetGraphicsStyleManager();
    ElementId id1 = pGStyleManager->GetGraphicsStyleIdByCategoryUid(CustomizerGStyleManager::s_category_id_CategoryC);
    ElementId id2 = pGStyleManager->GetGraphicsStyleIdByCategoryUid(CustomizerGStyleManager::s_category_id_CategoryD);

    double viewScale = pModelView->GetViewScale();
    double scale = 1 / viewScale;
    std::vector<ElementId> styleIds;

    // 功能实现：根据不同视图比例，返回不同样式
    if (MathUtils::IsGreaterThanOrEqual(scale, 100))
    {
        info.CustomizeElementGraphicsStyle(GraphicsStyleOverrideGrade::Document, GraphicsStyleActiveRange::Background, { id1 });
        return info;
    }

    info.CustomizeElementGraphicsStyle(GraphicsStyleOverrideGrade::Document, GraphicsStyleActiveRange::Background, { id2 });
    return info;
}

